package com.chaodroid.flyui.widget.navigation;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;

import com.chaodriod.common.utils.screen.ScreenUtils;

/**
 * Created by CHEN on 2017/12/3.
 */

public class BottomNavigation extends LinearLayout {

    private static final String TAG = "BottomNavigation";
    private Context mContext;
    private int mItemCount = 0;

    public BottomNavigation(Context context) {
        this(context, null);
    }

    public BottomNavigation(Context context, @Nullable AttributeSet attrs) {
        //设置默认样式
        this(context, attrs, 0);
    }

    public BottomNavigation(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init(attrs);
    }

    private void init(@Nullable AttributeSet attrs) {
        //判断是否在布局文件中设置了 Gravity 属性
        boolean hasGravity = false;
        boolean hasPadding = false;
        boolean hasBackground = false;
        int count = attrs.getAttributeCount();
        for (int i = 0; i < count; i++) {
            String attrName = attrs.getAttributeName(i);
            if ("gravity".equals(attrName)) {
                hasGravity = true;
            } else if ("padding".equals(attrName) ||
                    "paddingTop".equals(attrName) ||
                    "paddingBottom".equals(attrName) ||
                    "paddingLeft".equals(attrName) ||
                    "paddingRight".equals(attrName)) {
                hasPadding = true;
            } else if ("background".equals(attrName)) {
                hasBackground = true;
            }
        }
        //Gravity 默认设置为 CENTER
        if (!hasGravity) {
            setGravity(Gravity.CENTER);
        }
        if (!hasPadding) {
            setPadding(0, ScreenUtils.dp2px(mContext, 10), 0, ScreenUtils.dp2px(mContext, 10));
        }
        if (!hasBackground) {
            setBackgroundColor(0xFFCCCCCC);
        }
    }

    public void setAdapter(Adapter adapter, final OnItemClickListener listener) {
        if (adapter != null) {
            mItemCount = adapter.getItemCount();
            if (mItemCount > 0) {
                for (int i = 0; i < mItemCount; i++) {
                    final View item = adapter.bindItemView(i);
                    item.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
                    addView(item);
                    if (listener != null) {
                        final int finalI = i;
                        item.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                listener.onItemClick(v, finalI);
                            }
                        });
                    }
                }
                requestLayout();
            }
        }
    }

    public abstract static class Adapter {

        public abstract int getItemCount();

        @NonNull
        public abstract View bindItemView(int position);
    }

    public interface OnItemClickListener {
        void onItemClick(View item, int position);
    }

}
